package com.ruoyi.tongban.service.impl;

import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.tongban.domain.UsrMember;
import com.ruoyi.tongban.domain.UsrParentalChild;
import com.ruoyi.tongban.mapper.UsrMemberMapper;
import com.ruoyi.tongban.mapper.UsrParentalChildMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tongban.mapper.UsrParentalMapper;
import com.ruoyi.tongban.domain.UsrParental;
import com.ruoyi.tongban.service.IUsrParentalService;

/**
 * 家长,关联member,为家长的扩展信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-02
 */
@Service
public class UsrParentalServiceImpl implements IUsrParentalService 
{
    @Autowired
    private UsrParentalMapper usrParentalMapper;

    @Autowired
    private UsrParentalChildMapper usrParentalChildMapper;

    @Autowired
    private UsrMemberMapper usrMemberMapper;

    /**
     * 查询家长,关联member,为家长的扩展信息
     * 
     * @param parentalId 家长,关联member,为家长的扩展信息ID
     * @return 家长,关联member,为家长的扩展信息
     */
    @Override
    public UsrParental selectUsrParentalById(Long parentalId)
    {
        return usrParentalMapper.selectUsrParentalById(parentalId);
    }

    /**
     * 查询家长,关联member,为家长的扩展信息列表
     * 
     * @param usrParental 家长,关联member,为家长的扩展信息
     * @return 家长,关联member,为家长的扩展信息
     */
    @Override
    public List<UsrParental> selectUsrParentalList(UsrParental usrParental)
    {
        return usrParentalMapper.selectUsrParentalList(usrParental);
    }

    /**
     * 新增家长,关联member,为家长的扩展信息
     * 
     * @param usrParental 家长,关联member,为家长的扩展信息
     * @return 结果
     */
    @Override
    public int insertUsrParental(UsrParental usrParental)
    {
        UsrMember usrMember = new UsrMember();
        if (usrParental.getMemberAccount() == null){
            throw new CustomException("添加失败,请先添加用户名！");
        }
        UsrMember result = usrMemberMapper.findByMemberAccount(usrParental.getMemberAccount());
        if(result != null){
            throw new CustomException("添加失败,添加的"+usrParental.getMemberAccount()+"已存在,请更换用户名！");
        }
        usrMember.setMemberType(2);
        usrMember.setMemberAccount(usrParental.getMemberAccount());
        usrMember.setMemberMobile(usrParental.getParentalPhone());
        usrMember.setMemberPass(Md5Utils.hash("123456"));
        usrMember.setMemberNick(usrParental.getMemberNick());
        usrMember.setItemId(-1L);
        usrMember.setStatus(1);
        usrMember.setCreateTime(DateUtils.getNowDate());
        usrMember.setCreateUser(SecurityUtils.getLoginUser().getUser().getUserId());
        usrMemberMapper.insertUsrMember(usrMember);

        usrParental.setParentalId(usrMember.getMemberId());
        usrParental.setParentalSex(usrParental.getParentalSex());
        usrParental.setParentalBirthday(usrParental.getParentalBirthday());
        usrParental.setParentalIdcard(usrParental.getParentalIdcard());
        usrParental.setParentalPhone(usrParental.getParentalPhone());
        usrParental.setParentalEducational(usrParental.getParentalEducational());
        usrParental.setParentalIscpc(usrParental.getParentalIscpc());
        usrParental.setParentalWorkunit(usrParental.getParentalWorkunit());
        usrParental.setCreateUser(SecurityUtils.getLoginUser().getUser().getUserId());
        usrParental.setCreateTime(DateUtils.getNowDate());
        int index = usrParentalMapper.insertUsrParental(usrParental);

        usrMember.setItemId(usrMember.getMemberId());
        usrMemberMapper.updateUsrMember(usrMember);

        UsrParentalChild usrParentalChild = new UsrParentalChild();
        usrParentalChild.setMemberId(usrMember.getMemberId());
        usrParentalChild.setChildId(usrParental.getChildId());
        usrParentalChild.setFamilyId(-1L);
        usrParentalChild.setStatus(1);
        usrParentalChild.setParentRoleName("测试");
        usrParentalChild.setParentRole(-1l);
        usrParentalChild.setCreateUser(SecurityUtils.getLoginUser().getUser().getUserId());
        usrParentalChild.setCreateTime(DateUtils.getNowDate());
        usrParentalChildMapper.insertUsrParentalChild(usrParentalChild);
        return index;
    }

    /**
     * 修改家长,关联member,为家长的扩展信息
     * 
     * @param usrParental 家长,关联member,为家长的扩展信息
     * @return 结果
     */
    @Override
    public int updateUsrParental(UsrParental usrParental)
    {
        UsrMember usrMember = new UsrMember();
        usrMember.setMemberType(2);
        usrMember.setMemberAccount(usrParental.getMemberAccount());
        usrMember.setMemberMobile(usrParental.getParentalPhone());
        usrMember.setMemberNick(usrParental.getMemberNick());
        usrMember.setItemId(-1L);
        usrMember.setStatus(1);
        usrMember.setCreateTime(DateUtils.getNowDate());
        usrMember.setCreateUser(SecurityUtils.getLoginUser().getUser().getUserId());
        usrParental.setUpdateTime(DateUtils.getNowDate());
        usrMemberMapper.updateUsrMember(usrMember);

        usrParental.setParentalId(usrMember.getMemberId());
        usrParental.setParentalSex(usrParental.getParentalSex());
        usrParental.setParentalBirthday(usrParental.getParentalBirthday());
        usrParental.setParentalIdcard(usrParental.getParentalIdcard());
        usrParental.setParentalPhone(usrParental.getParentalPhone());
        usrParental.setParentalEducational(usrParental.getParentalEducational());
        usrParental.setParentalIscpc(usrParental.getParentalIscpc());
        usrParental.setParentalWorkunit(usrParental.getParentalWorkunit());
        usrParental.setUpdateUser(SecurityUtils.getLoginUser().getUser().getUserId());
        usrParental.setUpdateTime(DateUtils.getNowDate());
        int index = usrParentalMapper.updateUsrParental(usrParental);

        UsrParentalChild usrParentalChild = new UsrParentalChild();
        usrParentalChild.setMemberId(usrMember.getMemberId());
        usrParentalChild.setChildId(usrParental.getChildId());
        usrParentalChild.setFamilyId(-1L);
        usrParentalChild.setStatus(1);
        usrParentalChild.setParentRoleName("测试");
        usrParentalChild.setParentRole(-1l);
        usrParentalChild.setUpdateUser(SecurityUtils.getLoginUser().getUser().getUserId());
        usrParentalChild.setUpdateTime(DateUtils.getNowDate());
        usrParentalChildMapper.updateUsrParentalChild(usrParentalChild);

        return index;
    }

    /**
     * 批量删除家长,关联member,为家长的扩展信息
     * 
     * @param parentalIds 需要删除的家长,关联member,为家长的扩展信息ID
     * @return 结果
     */
    @Override
    public int deleteUsrParentalByIds(Long[] parentalIds)
    {
        return usrParentalMapper.deleteUsrParentalByIds(parentalIds);
    }

    /**
     * 删除家长,关联member,为家长的扩展信息信息
     * 
     * @param parentalId 家长,关联member,为家长的扩展信息ID
     * @return 结果
     */
    @Override
    public int deleteUsrParentalById(Long parentalId)
    {
        /*UsrParental result = usrParentalMapper.selectUsrParentalById(parentalId);
        if(result.getParentalId() == null){
            throw new CustomException("未查到此用户信息！");
        }
        usrParentalChildMapper.deleteUsrParentalChildById(parentalId);
        usrMemberMapper.deleteUsrMemberById(parentalId);*/
        return usrParentalMapper.deleteUsrParentalById(parentalId);
    }
}
